//
//  UserContactViewController.m
//  HardWare5
//
//  Created by cz on 2016/12/14.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "UserContactViewController.h"

@interface UserContactViewController ()
@property (weak, nonatomic) IBOutlet UILabel *contcatLabel;
@property (weak, nonatomic) IBOutlet UIButton *phoneButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;


@end

@implementation UserContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //给个返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(goback)];
    
    NSString *phoneStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneStr"];
    
    if (phoneStr) {
        [self.phoneButton setTitle:F(@"联系电话:%@", phoneStr) forState:UIControlStateNormal];
        [self.phoneButton sizeToFit];
    }
    
    [self.phoneButton addTarget:self action:@selector(phoneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self getContactData];
    [self getPhoneData];
}

- (void)phoneButtonClick {
    
    NSString *phoneStr = self.phoneButton.titleLabel.text;
    NSString *phoneNum = [phoneStr substringFromIndex:5];
    NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",phoneNum];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

- (void)getContactData {
    NSDictionary *dict = nil;
    NSString *url = F(@"%@Index/getLianxi", BASEURL);
    
    [FSServer requestWithTarget:url param:dict result:^(id responseObject, NSError *error) {
        
        NSDictionary *d = responseObject[@"data"];
        
        if ([responseObject[@"flag"] intValue] == 1 & d.count != 0) {
            
            self.contcatLabel.text = d[@"value"];
            self.nameLabel.text = d[@"name"];
            self.addressLabel.text = d[@"address"];
            [self.contcatLabel sizeToFit];
        }else {
            [MRHud showWithFailure:@"您的网络不给力"];
        }
            
    }];
}

- (void)getPhoneData {
    NSDictionary *dict = nil;
    NSString *url = F(@"%@Index/getmobile", BASEURL);
    
    [FSServer requestWithTarget:url param:dict result:^(id responseObject, NSError *error) {
        
        NSDictionary *d = responseObject[@"data"];
        
        if ([responseObject[@"flag"] intValue] == 1 & d.count != 0) {
            
            NSString *phoneStr = d[@"value"];
            [[NSUserDefaults standardUserDefaults] setObject:phoneStr forKey:@"phoneStr"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [self.phoneButton setTitle:F(@"联系电话:%@", phoneStr) forState:UIControlStateNormal];
            [self.phoneButton sizeToFit];
            
        }else {
            [MRHud showWithFailure:@"您的网络不给力"];
        }
    }];
}

- (void)goback {

    [self.navigationController popViewControllerAnimated:YES];
}

@end
